#include "login.h"
#include <myHeader.h>
#include <shadow.h>
#include <string.h>

int login(usrInfo_t *pUsrInfo){
    // 接收用户名长度
    int len;
    char name[16];
    recv(pUsrInfo->netFd,&len,sizeof(int),MSG_WAITALL);
    recv(pUsrInfo->netFd,name,len,MSG_WAITALL);
    struct spwd *info=getspnam(name);
    char *psalt;
    psalt=strrchr(info->sp_pwdp,'$');
    int saltLen=psalt-info->sp_pwdp;
    char salt[32]={0};
    strncpy(salt,info->sp_pwdp,saltLen);
    printf("盐值长度%d\n",saltLen);
    printf("盐值%s\n",salt);
    send(pUsrInfo->netFd,&saltLen,sizeof(int),0);
    send(pUsrInfo->netFd,info->sp_pwdp,saltLen,0);
    int pwdLen;
    char pwd[128]={0};
    recv(pUsrInfo->netFd,&pwdLen,sizeof(int),MSG_WAITALL);
    recv(pUsrInfo->netFd,pwd,pwdLen,MSG_WAITALL);
    int ret=strcmp(info->sp_pwdp,pwd);
    /* printf("ret==%d\n",ret); */
    if(ret==0){
        // 通过验证
        send(pUsrInfo->netFd,&ret,sizeof(int),0);
        return 0;
    }
    else{
        send(pUsrInfo->netFd,&ret,sizeof(int),0);
        return -1;
    }
}

